package com.yiwei.zhh.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yiwei.zhh.R;
import com.yiwei.zhh.activity.WechatChatActivity;
import com.yiwei.zhh.adapter.MyPrizeAdapter;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.PrizeListBean;
import com.yiwei.zhh.api.bean.PrizeTakeBean;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.OnLoadeMoreListener;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.Util;
import com.yiwei.zhh.view.Dialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-7-23.
 */

public class MyPrizeFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private MyPrizeAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getPrizeList(page);
        }
    };
    private int type;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_prize;
    }

    @Override
    protected void initView() {
        type = getArguments().getInt("type");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getPrizeList(1);
            }
        });
        adapter = new MyPrizeAdapter(getContext());
        adapter.setFstatus(type);
        adapter.setOnChooseListener(new MyPrizeAdapter.OnChooseListener() {
            @Override
            public void onClick(String id) {
                getPrizeTake(id);
            }
        });
        list.setAdapter(adapter);
        list.addOnScrollListener(onLoadeMoreListener);
        getPrizeList(1);
    }

    public void getPrizeList(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.makeText(getContext(), "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        PrizeListBean bean = new PrizeListBean();
        bean.page = page;
        bean.status = type;
        Api.api_service.getPrizeLists(bean).compose(RxLifeUtil.checkOn(getContext())).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    adapter.add(result.data, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    public void getPrizeTake(String id) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.makeText(getContext(), "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        PrizeTakeBean bean = new PrizeTakeBean();
        bean.id = id;
        Api.api_service.getPrizeTake(bean).compose(RxLifeUtil.checkOn(getContext())).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    Dialog.showDefaultV3Dialog(getContext(), "领取成功", "恭喜您奖品领取成功\n你可以联系客服去兑换大奖啦！", "取消", "联系客服", new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {
                            Util.startActivity(getContext(), WechatChatActivity.class);
                        }

                        @Override
                        public void cancel() {

                        }
                    });
                    onLoadeMoreListener.setPage();
                    getPrizeList(1);
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
